﻿package com.eaglesandcobras.games.questions{
	import flash.display.MovieClip;
	import flash.text.TextFieldAutoSize;
	import com.eaglesandcobras.games.questions.QuestionButton;
	import flash.events.MouseEvent;
	import flash.events.Event;
	
	public class Question extends MovieClip{
		public var bg:MovieClip;
		public var incorrectClip:MovieClip;
		public var correctClip:MovieClip;
		public var questionContent:MovieClip;
		private var _padding:Number;
		private var _answers:Array;
		
		public function Question() {
			super();
			hideParts();
		}
		
		private function hideParts():void {
			incorrectClip.visible = false;
			correctClip.visible = false;
			
			incorrectClip.questionDoneBtn.addEventListener(MouseEvent.CLICK, allDone);
			correctClip.questionDoneBtn.addEventListener(MouseEvent.CLICK, allDone);
			
			incorrectClip.questionDoneBtn.buttonMode = true;
			correctClip.questionDoneBtn.buttonMode = true;
		}
		
		public function set incorrectStatement($s:String):void {
			incorrectClip.copyTxt.text = $s;
			incorrectClip.copyTxt.wordWrap = true;
			incorrectClip.copyTxt.autoSize = TextFieldAutoSize.CENTER;
			
			incorrectClip.questionDoneBtn.y = incorrectClip.copyTxt.y+incorrectClip.copyTxt.height+20;
		}
		
		public function set correctStatement($s:String):void {
			correctClip.copyTxt.text = $s;
			correctClip.copyTxt.wordWrap = true;
			correctClip.copyTxt.autoSize = TextFieldAutoSize.CENTER;
			
			correctClip.questionDoneBtn.y = correctClip.copyTxt.y+correctClip.copyTxt.height+20;
		}
		
		public function set question($s:String):void {
			questionContent.questionTxt.text = $s;
			questionContent.questionTxt.wordWrap = true;
			questionContent.questionTxt.autoSize = TextFieldAutoSize.CENTER;
		}
		
		public function set answers($a:Array):void {
			_answers = new Array();
			
			for ( var i=0;i<$a.length;i++) {
				_answers[i] = new QuestionButton();
				questionContent.addChild( _answers[i] );
				_answers[i].y = questionContent.questionTxt.y + questionContent.questionTxt.height + i*(_answers[i].height+5)+20;
				_answers[i].x = bg.width/2-_answers[i].width/2;
				_answers[i].label = $a[i];
			}
			
			bg.height = _answers[_answers.length-1].y+_answers[_answers.length-1].height+questionContent.y*2+10;
		}
		
		public function get answers():Array {
			return _answers;
		}
		
		private function allDone(e:MouseEvent):void {
			this.dispatchEvent( new Event (Event.COMPLETE) );
		}
		
	}
}
